# -*- coding:utf-8 -*-
import unittest
import os, os.path, glob, re, sys
import shutil


class FileUtil(unittest.TestCase):
    """
    Python提供了os模块、os.path模块处理文件。
    """

    def test_newFile(self):
        """
        创建文件
        mode说明：r,只读。r+读写。w：写，覆盖。w+.a:写入，追加。a+:
        :return:
        """
        file_name = os.getcwd() + "/a.txt"
        print("文件名"+file_name)
        file = open(file_name, mode="a+", buffering=50, encoding="utf-8")
        file.write("mode说明：r,只读。r+读写。w：写，覆盖。w+.a:写入，追加。a+:")
        file.truncate(13)

        print(file.name)
        print("输入文件编码%s" % file.encoding)
        file.close()

    def test_readLine(self):
        file_name = os.getcwd() + "/a.txt"
        f = open(file_name, encoding="utf-8")
        while True:
            line = f.readline(50)
            if line:
                print(line)
            else:
                break

    def test_readLines(self):
        file_name = os.getcwd() + "/a.txt"
        f = open(file_name, encoding="utf-8")
        line = f.readlines(50)
        print(str(line))

    def test_writeLines(self):
        file_name = os.getcwd() + "/a.txt"
        f = open(file_name, encoding="utf-8",mode="a+")
        lines = ['mode说明mod\n', '写入文件第二行\n']
        f.writelines(lines)
        f.close()

        pass

    def test_delFile(self):
        file_name = os.getcwd() + "/a.txt"
        if os.path.exists(file_name):
            os.remove(file_name)

    def test_rename(self):
        file_name = os.getcwd() + "/a.txt"
        # file = open(file_name, mode="a+", buffering=50, encoding="utf-8")
        os.rename(file_name, os.getcwd() + "/b.txt")

    def test_copy(self):
        sdir = os.getcwd() + "/a.txt"
        ddir = os.getcwd() + "/b.txt"
        shutil.copy(sdir, ddir)
        shutil.move(sdir, "../m.txt")

    def test_path(self):
        file_name = os.getcwd() + "/a.txt"
        file = open(file_name, mode="a+", buffering=50, encoding="utf-8")
        # 返回指定路径的绝对路径、目录，文件是否存在
        print(os.path.abspath(file_name))
        print(os.path.dirname(file_name))
        print(os.path.exists(file_name))
        print(os.path.split(file_name))  # 分割路径，最后一个为文件名
        print(os.path.splitext(file_name))  # 分割文件扩展名

        print(os.path.isdir(file_name))  # 判读是否文件，目录
        print(os.path.isfile(file_name))

        print(os.path.getsize(file_name))  # 获取文件大小
        print(os.path.getctime(file_name))  # 获取文件创建时间、更新时间、访问时间

        print(list(os.walk(os.getcwd())))
        print(glob.glob("*.txt"))
        print(glob.glob("c:/*/*.txt"))
        print(os.path.abspath(os.curdir))
        print(os.path.abspath("../.."))  # 获取当前父目录

        # D:\WORKSPACE\Python2.7\jtalk_windows_auto\jtalk\comm
        # D:\WORKSPACE\Python2.7\jtalk_windows_auto
        print(sys.path[0])  # 脚本执行时路径，同os.getcwd
        print(sys.path[1])  # 工程根目录/

    def test_replace(self):
        file_name = os.getcwd() + "/a.txt"
        print(file_name)
        file = open(file_name,  encoding="utf-8")
        file2 = open("c.txt", mode="a+", encoding="utf-8")
        while True:
            line = file.readline(50)
            if line:
                print(re.findall("mod", line))
                file2.write(line.replace("m","a"))
            else:
                break

    def test_dir(self):
        os.getcwd()  # 获取当前目录
        os.listdir("dir2")  # 获取目录下的文件名列表
        os.mkdir("dir1")
        os.makedirs("dir2/dir22")
        os.rmdir("dir2/dir24")  # 删除单级目录
        os.removedirs("dir2")   # 需要判空
        os.removedirs("dir2/dir22")  # 删除多级目录


